// import {  createSlice, Draft, PayloadAction } from "@reduxjs/toolkit";
// import { persistReducer } from "redux-persist";
// import storage from "redux-persist/lib/storage";

// // Partial<T> 是一个内置的工具类型，它的作用是将类型 T 里的所有属性转换为可选属性
// export interface UserSlice {
//   user: Partial<User>;
// }

// const initialState:UserSlice=  {
//     user: {},
//   }
// const { reducer: userReducer, actions } = createSlice({
//   name: "user",
//   initialState:initialState,
//   reducers: {
//     saveUser(state:Draft<UserSlice>, action: PayloadAction<Partial<User>>) {
//       state.user = action.payload;
//     },
//   },
// });

// // 持久化 保存到本地存储 
// const persistUserReducer = persistReducer(
//   { key: "userReducer", storage },
//   userReducer
// );
// export const { saveUser } = actions;
// export default persistUserReducer;
// src/store/slice/user.ts
import { createSelector, createSlice, Draft, PayloadAction } from "@reduxjs/toolkit";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import { AppState } from "@store/index";

export interface UserSlice {
  user: Partial<User>;
}

const { reducer: userReducer, actions } = createSlice({
  name: "user",
  initialState: {
    user: {},
  },
  reducers: {
    saveUser(state, action) {
      state.user = action.payload;
    },
  },
});

// 持久化
const persistUserReducer = persistReducer(
  { key: "userReducer", storage },
  userReducer
);

export default persistUserReducer;
export const { saveUser } = actions;